home *** CD-ROM | disk | FTP | other *** search
- /* File: HowToHideApp.c
-
- Description:
- This sample illustrates how the SetHideOnSwitch and GetHideOnSwitch
- routines can be used to hide an application. These routines were first
- documented in Technote TN1102, "Mac OS 8". On the world wide
- web, documentation for these routines can be found at the address:
-
- http://developer.apple.com/technotes/tn/tn1102.html#processmgr
-
- This file contains a sample application that calls the routines defined
- in HideCalls.h to hide itself.
-
- Copyright:
- Copyright © 1999 by Apple Computer, Inc.
- All rights reserved.
-
- Disclaimer:
- You may incorporate this sample code into your applications without
- restriction, though the sample code has been provided "AS IS" and the
- responsibility for its operation is 100% yours. However, what you are
- not permitted to do is to redistribute the source as "DSC Sample Code"
- after having made changes. If you're going to re-distribute the source,
- we require that you make it clear in the source that the code was
- descended from Apple Sample Code, but that you've made changes.
-
- Change History (most recent first):
- 12/6/1999 created
- */
-
- #include <Types.h>
- #include <QuickDraw.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Events.h>
- #include <Fonts.h>
- #include <SegLoad.h>
- #include <Resources.h>
- #include <Gestalt.h>
- #include <Appearance.h>
-
- #include "HideCalls.h"
-
- /* true while the app is running */
- Boolean gRunning = true;
-
- /* true while the app is in forground */
- Boolean gForground = true;
-
- /* true if appearance exists */
- Boolean gAppearance = false;
-
- /* the main window */
- DialogPtr gMainWindow;
-
- /* the 'hide' button */
- ControlHandle gHideButton;
-
- /* QuickDraw globals*/
- #ifndef __MWERKS__
- QDGlobals qd;
- #endif
-
-
- /* MyChooseP is a callback routine that the HowToHideApp provides as a
- parameter to the HideMe routine. Currently, this routine selects
- the Finder as the next process to be switched into the forground
- when the HowToHideApp is hidden. */
- static OSStatus MyChooseP(ProcessSerialNumber *myPSN, ProcessSerialNumber *targetPSN) {
- ProcessInfoRec pInfo;
- ProcessSerialNumber iteratingPSN;
- OSStatus err;
- /* iterate through all active processes */
- iteratingPSN.highLongOfPSN = iteratingPSN.lowLongOfPSN = kNoProcess;
- while (GetNextProcess(&iteratingPSN) == noErr) {
- /* get the process information */
- pInfo.processInfoLength = sizeof(pInfo);
- pInfo.processName = NULL;
- pInfo.processAppSpec = NULL;
- err = GetProcessInformation(&iteratingPSN, &pInfo);
- if (err != noErr) return err;
- /* if it's the Finder, return its process serial number */
- if (pInfo.processSignature == 'MACS') {
- *targetPSN = iteratingPSN;
- return noErr;
- }
- }
- /* Finder not found */
- return procNotFound;
- }
-
-
- /* MyEventHandler is the main event handler for the HowToHideApp
- application. It is called for every event returned by
- WaitNextEvent, and it is also provided as a parameter to the
- HideMe routine when the HowToHideApp is being hidden. Basically,
- this routine draws the dialog and calls HideMe whenever
- the "Hide Me Now" button is clicked. */
- static OSStatus MyEventHandler(EventRecord *ev) {
- OSStatus err;
- /* track forground switches */
- if ((ev->what == osEvt) && (((ev->message >> 24) & 0x0FF) == suspendResumeMessage)) {
- gForground = ((ev->message & resumeFlag) != 0);
- DrawDialog(gMainWindow);
- HiliteControl(gHideButton, gForground ? 0 : 255);
- }
- /* redraw if we're activating */
- if (ev->what == activateEvt) {
- DrawDialog(gMainWindow);
- HiliteControl(gHideButton, ((ev->modifiers & activeFlag) != 0) ? 0 : 255);
- }
- /* handle clicks in the dialog window */
- if (IsDialogEvent(ev)) {
- DialogPtr theDialog;
- short itemNo;
- if (DialogSelect(ev, &theDialog, &itemNo)) {
-
- HiliteControl(gHideButton, 255); /* HideMe is not re-entrant */
-
- /* hide the application */
- err = HideMe(MyEventHandler, MyChooseP);
-
- }
- }
- /* process other window commands */
- if (ev->what == mouseDown) {
- WindowPtr theWindow;
- switch (FindWindow(ev->where, &theWindow)) {
-
- /* clicks in the close box, close the app */
- case inGoAway:
- if (TrackGoAway(theWindow, ev->where))
- gRunning = false;
- break;
-
- /* allow window drags */
- case inDrag:
- { Rect boundsRect = { -32000, -32000, 32000, 32000};
- DragWindow(theWindow, ev->where, &boundsRect);
- }
- break;
-
- /* desktop clicks, etc... */
- case inSysWindow:
- SystemClick(ev, theWindow);
- break;
- }
- }
- /* if we're not running, return userCanceledErr. This allows
- the user to abort the HideMe by closing the window if it gets stuck. */
- if (gRunning)
- return noErr;
- else return userCanceledErr;
- }
-
-
-
-
-
-
-
-
-
- int main(void) {
- OSErr err;
- long response;
- /* set up our app */
- SetApplLimit(GetApplLimit());
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- TEInit();
- InitMenus();
- InitDialogs(0);
- FlushEvents(everyEvent, 0);
- InitCursor();
- if (Gestalt(gestaltAppearanceAttr, &response) != noErr) response = 0;
- if ((response & (1<<gestaltAppearanceExists)) != 0) {
- err = RegisterAppearanceClient();
- if (err != noErr) goto bail;
- gAppearance = true;
- }
-
- /* set up the dialog */
- gMainWindow = GetNewDialog(128, NULL, (WindowPtr) (-1));
- { short itemt;
- Rect itemb;
- GetDialogItem(gMainWindow, 1, &itemt, (Handle*) &gHideButton, &itemb);
- }
-
- /* run the app */
- while (gRunning) {
- EventRecord ev;
-
- /* get the next event */
- if ( ! WaitNextEvent(everyEvent, &ev, GetCaretTime(), NULL))
- ev.what = nullEvent;
-
- /* pass the event along to the event handler */
- err = MyEventHandler(&ev);
- if (err != noErr) goto bail;
- }
-
- bail:
- if (gAppearance)
- UnregisterAppearanceClient();
- ExitToShell();
- return 0;
- }